<template>
    <el-card>
        <!--设置 inline 属性为 true 可以让表单域变为行内  -->
        <el-form :inline="true">
            <el-form-item label="一级分类">
                <el-select :disabled="scene == 0 ? false : true" v-model="categoryStore.c1Id" @change="getTwoData">
                    <!-- option中：label->展示数据 value->为select下拉菜单收集的数据 -->
                    <el-option v-for="(c1, index) in categoryStore.c1Arr" :key="c1.id" :value="c1.id"
                        :label="c1.name"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="二级分类">
                <el-select :disabled="scene == 0 ? false : true" v-model="categoryStore.c2Id" @change="getThreeData">
                    <el-option v-for="(c2, index) in categoryStore.c2Arr" :key="c2.id" :value="c2.id"
                        :label="c2.name"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="三级分类">
                <el-select :disabled="scene == 0 ? false : true" v-model="categoryStore.c3Id">
                    <el-option v-for="(c3, index) in categoryStore.c3Arr" :key="c3.id" :value="c3.id"
                        :label="c3.name"></el-option>
                </el-select>
            </el-form-item>
        </el-form>
    </el-card>
</template>
<script setup lang="ts">
//引入接口
import { onMounted } from 'vue'
//引入商品分类仓库
import useCategoryStore from '@/store/modules/category'
let categoryStore = useCategoryStore()

//接收父传过来的scene
defineProps(['scene'])

//商品分类全局组件挂载完毕，通过仓库发请求获取一级分类数据
onMounted(() => {
    //获取一级分类数据
    getC1()
})

const getC1 = () => {
    //通知仓库发请求
    categoryStore.getC1()
}

//select的change事件的回调：选中值的时候触发(确保有一级分类的id)
const getTwoData = () => {
    //选择改变时，需要将显示的二级三级分类数据清空
    categoryStore.c2Id = '',
        categoryStore.c3Arr = [],
        categoryStore.c3Id = ''
    //通知仓库获取二级分类数据
    categoryStore.getC2()
}

const getThreeData = () => {
    //选择改变时，需要将显示的三级分类数据清空
    categoryStore.c3Id = ''
    //通知仓库获三级分类数据
    categoryStore.getC3()
}
</script> 


<style scoped></style>
